package cn.yeoman.flowcontrol.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import cn.yeoman.flowcontrol.dal.dao.AffairTypeDAO;
import cn.yeoman.flowcontrol.dal.entity.AffairType;
import cn.yeoman.flowcontrol.service.AffairTypeService;

@Service("affairTypeService")
public class AffairTypeServiceImpl implements AffairTypeService {

	@Resource(name = "affairTypeDAO")
	private AffairTypeDAO affairTypeDAO;
	
//	@Resource(name = "flowService")
//	private FlowService flowService;

	@Override
	public boolean add(AffairType affairType) {
		affairType.setGmtCreate(new Date());
		return affairTypeDAO.add(affairType);
	}

	@Override
	public boolean delete(String typeCode) {
//		return flowService.findCountByTypeCode(typeCode) == 0 ? affairTypeDAO.delete(typeCode) : false;
		return false;
	}

	@Override
	public boolean update(AffairType affairType) {
		affairType.setGmtModified(new Date());
		return affairTypeDAO.update(affairType);
	}

	@Override
	public AffairType findAffairType(String typeCode) {
		List<AffairType> affairTypeList = queryTypeList();
		if(affairTypeList != null && !affairTypeList.isEmpty()){
			for(AffairType affairType : affairTypeList)
				if(typeCode.equals(affairType.getTypeCode()))
					return affairType;
		}
		return null;
	}
	
	@Override
	public String findTypeName(String typeCode) {
		for(AffairType affairType : queryTypeList())
			if(typeCode.equals(affairType.getTypeCode()))
				return affairType.getTypeName();
		return null;
	}
	
	@Override
	public List<AffairType> queryTypeList() {
		return affairTypeDAO.getTypeList();
	}

	@Override
	public List<AffairType> findTypeListByParams(String typeName, Character status) {
		List<AffairType> returnList = new ArrayList<AffairType>();
		List<AffairType> affairTypeList = queryTypeList();
		for(AffairType affairType : affairTypeList){
			if(StringUtils.isNotBlank(typeName) && !affairType.getTypeName().contains(StringUtils.trimToNull(typeName)))
				continue;
			if(status != null && !affairType.getStatus().equals(status))
				continue;
			returnList.add(affairType);
		}
		return returnList.isEmpty() ? null : returnList;
	}

}
